package com.decent.ejfadmin.user.dao.provider;


import com.decent.ejfadmin.common.bean.AdminInterface;
import com.decent.ejfadmin.common.bean.Page;
import com.decent.ejfadmin.utils.SQLWrapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;

/**
 * 交接班
 *
 * @author weicx
 * @date 2018/07/26
 */
public class AdminInterfaceDaoProvider {
    /**
     * 交接班页面查询SQL
     *
     * @param map
     * @return
     */
    public String queryTitleList(HashMap<String, Object> map) {
        Page page = (Page) map.get("page");
        AdminInterface adminInterface = (AdminInterface) map.get("adminInterface");
        StringBuilder sql = new StringBuilder();
        sql.append("select id,ADMIN_NAME adminName,BIND_ROLE_IDS bindRoleIds,TITLE,CONTENT,TYPE,DATE_FORMAT(VALIDITY,'%Y-%m-%d %H:%i:%S') VALIDITY,\n" +
                "  DATE_FORMAT(SEND_TIME,'%Y-%m-%d %H:%i:%S')  sendTime,UPDATE_TIME updateTime,update_admin updateAdmin" +
                " from YUHUANG_ADMINWORK where SEND_TIME >= #{startTime} and SEND_TIME <= #{endTime} " +
                " and ADMIN_TYPE = #{adminInterface.adminType}");
        if (StringUtils.isNotBlank(adminInterface.getAdminName())) {
            sql.append(" and instr(ADMIN_NAME,#{adminInterface.adminName})>0");
        }
        if (StringUtils.isNotBlank(adminInterface.getContent())) {
            sql.append(" and instr(CONTENT ,#{adminInterface.content})>0");
        }
        sql.append(" order by TYPE,SEND_TIME desc ");

        return SQLWrapUtils.wrapPage(sql, page.getBeginIndex(), page.getSize());
    }

    public String queryTitleListCount(HashMap<String, Object> map) {
        StringBuilder sql = new StringBuilder();
        AdminInterface adminInterface = (AdminInterface) map.get("adminInterface");
        sql.append("select count(*)  from YUHUANG_ADMINWORK where SEND_TIME >= #{startTime} and SEND_TIME <= #{endTime} " +
                "and ADMIN_TYPE = #{adminInterface.adminType} ");
        if (StringUtils.isNotBlank(adminInterface.getAdminName())) {
            sql.append(" and instr(ADMIN_NAME,#{adminInterface.adminName})>0");
        }
        if (StringUtils.isNotBlank(adminInterface.getContent())) {
            sql.append(" and instr(CONTENT ,#{adminInterface.content})>0");
        }
        return sql.toString();
    }
}
